package org.rednebula.framework.core.service;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import org.rednebula.framework.core.exception.DbException;
import org.rednebula.framework.core.log.Log;
import org.springframework.stereotype.Component;


@Component
public class JdbcDbService extends BasicDbService implements IDbService, Serializable {

	static Log logger=Log.getLog(JdbcDbService.class);
	protected Connection getConnection(){
		return this.getSession().connection();
	}
	public int batchExecute(String hql, Object... values) {
		return 0;
	}

	public int batchExecute(String hql, Map<String, Object> values) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void delete(Object entity) {
		// TODO Auto-generated method stub

	}

	public void delete(Object id, Class<?> entityClass) {
		// TODO Auto-generated method stub

	}

	public <X> List<X> find(String hql, Object... values) {
		// TODO Auto-generated method stub
		return null;
	}

	public <X> X findUnique(String hql, Object... values) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object get(Object id, Class<?> entityClass) {
		// TODO Auto-generated method stub
		return null;
	}

	public void save(Object entity) {
		// TODO Auto-generated method stub

	}

	public void update(Object entity) {
		// TODO Auto-generated method stub

	}
	
	public Integer querySequence(String sequenceName){
		Statement statement=null;
		ResultSet set=null;
		String sql="SELECT "+sequenceName+".NEXTVAL FROM DUAL";
		try{
			statement=getConnection().createStatement();
	
			set=statement.executeQuery(sql);
			if(set.next()){
				return set.getInt(1);
			}
			return null;
		}catch(Exception g){
			g.printStackTrace();
			logger.error("error : "+sql, g.getCause());
			throw new DbException("序列 :"+sequenceName+" 不存在.");
		}finally{
			closeResultSet(set);
			closeStatement(statement);
		}
	}
	protected void closeResultSet(ResultSet set){
		try{
			if(set!=null)set.close();
		}catch(Exception g){}
	}
	protected void closeStatement(Statement statement){
		try{
			if(statement!=null)statement.close();
		}catch(Exception g){}
	}
}
